package com.NetSunWeiQin.chapter06;

public class SourceDemo {



    private static void  test01(){
        String protocol;
        String  spec = "echo://localhost:8000";
        int i, limit, c;
        int start = 0;
        String newProtocol ="";
        limit = spec.length();
        boolean aRef=false;
        boolean isRelative = false;
        for (i = start ; !aRef && (i < limit) &&
                ((c = spec.charAt(i)) != '/') ; i++) {
            if (c == ':') {

                String s = spec.substring(start, i).toLowerCase();
                if (isValidProtocol(s)) {
                    newProtocol = s;
                    start = i + 1;
                }
                break;
            }
        }

        // Only use our context if the protocols match.
        protocol = newProtocol;

    }


    private static  boolean isValidProtocol(String protocol) {
        int len = protocol.length();
        if (len < 1)
            return false;
        char c = protocol.charAt(0);
        if (!Character.isLetter(c))
            return false;
        for (int i = 1; i < len; i++) {
            c = protocol.charAt(i);
            if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' &&
                    c != '-') {
                return false;
            }
        }
        return true;
    }
}
